<?
Class DB {
	var $_debug;
	var $_handle;
	var $_last_query_string;
	
	//var $_tab;

	function & DB($a, $pconnect = 0, $debug = false) {
		$this->_debug	= $debug;
		if( $pconnect ){
			$this->_handle	= @mysql_pconnect($a['host'], $a['user'], $a['passwd']); 
		}else{
			$this->_handle	= @mysql_connect($a['host'], $a['user'], $a['passwd']);
		}
		if( mysql_errno()!=0 ){
			$this->_halt('Mysql Connect error', 'Connect(' . $pconnect . ') to MySQL (' . $a['host'] . ', ' . $a['user'] . ') failed');
		}
		if( $a['dbname'] ){
			if( !@mysql_select_db($a['dbname'], $this->_handle) ){
				$this->_halt('MySQL Select db Error', 'Cannot use database ' . $a['dbname'] );
			}
		}
		//$this->query("SET CHARACTER SET GBK");
	}

	function close() {
		return @mysql_close($this->_handle);
	}

	function &fetch($qry, $method = 1){
		$m = array(MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH);
		is_resource($qry) || $this->_halt('Mysql Fetch ERROR', 'Params is Not Resource.');
		return mysql_fetch_array($qry, $m[$method]);
	}

	function insert($t, $a) {
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		if( empty($a) ){
			$this->_halt('MySQL Insert Error', 'list is empty');
		}
		$this->split($a);
		if( empty($a) ){
			$this->_halt("MySQL Insert Error", 'list is not array or string');
		}
		$L = array();
		foreach($a as $k=>$v) {
			if( empty($v) ){
				unset($a[$k]);
			}else{
				$L[$v] = mysql_escape_string( $_POST[$v] );
			}
		}
		$query_string = 'INSERT INTO `' . $t . '` (`' . join('`,`', $a) . '`) VALUES (\'' . join('\',\'', $L) . '\')';
		$query	= $this->query($query_string);
		return @mysql_insert_id($this->_handle);
	}

	function &update($t, $a, $if) {
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		if( empty($a) ){
			$this->_halt('MySQL Update Error', 'list is empty');
		}
		$this->split($a);
		if( empty($a) ){
			$this->_halt('MySQL update Error', 'list is not array or string');
		}
		$L = array();
		foreach($a as $k=>$v) {
			if(	!empty($v)	){
				$L[$v] = '`' . $v . '`=\'' . mysql_escape_string( $_POST[$v] ) . '\'';
			}
		}
		$query_string = 'UPDATE  `' . $t . '` SET ' . join(',', $L) . ' WHERE ' . $if;
		$query = $this->query($query_string);
		return $query;
	}
	
	function & query($query_string, $method = 1) {	
		$this->_last_query_string	= $query_string;
		$query = $method && function_exists('mysql_unbuffered_query') ? @mysql_unbuffered_query($query_string, $this->_handle) : @mysql_query($query_string, $this->_handle);
		if (!$query) {
			$this->_halt('Query Error');
		}
		return $query;
	}

	function &all($t, $if = 1, $method = 1){
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		$query_string	= 'SELECT * FROM ' . $t . ' WHERE ' . $if ;
		$query = $this->query($query_string, $method);
		$ary = array();
		while( $l = $this->fetch($query) ){
			array_push($ary, $l);
		}
		return $ary;
	}

	function &ary($t, $if = 1, $o = '*', $ik = '', $iv = ''){
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		$query_string	= 'SELECT ' . $o . ' FROM ' . $t . ' WHERE ' . $if;
		$query = $this->query($query_string, 1);
		$ary = array();
		if(empty($ik)) {
			while( $v = $this->fetch($query) ){
				array_push($ary, $v);
			}
		}else{
			while( $v = $this->fetch($query) ){
				$ary[$v[$ik]] = $iv ? $v[$iv] : $v;
			}
		}
		return $ary;
	}

	function &one($t, $if, $o = '*'){
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		$query_string = 'SELECT ' . $o . ' FROM `' . $t . '` WHERE ' . $if ;
		$query = $this->query($query_string, 1);
		return $this->fetch($query);
	}

	function &page($t, $if = '1',  $ppl = 24, $lang = 1) {
		/*
		if(  empty($t)  ){
			$t = $this->_tab ;
		}else{ 
			$this->_tab = $t ;
		}
		*/
		$ppl 	= (int) $ppl ;
		if(	$ppl  < 1 || $ppl > 999 ){
			$ppl	= 24;
		}
		$query_string	= 'SELECT count(*) FROM ' . $t . ' WHERE ' . $if ;
		$query		= $this->query($query_string, 1) ;
		$le		= $this->fetch($query, 0);
		$len		= empty($le[0]) ? 1 : $le[0] ;
		$p		= array(
				'len'	=> $le[0],
				'ppl'	=> $ppl
		);
		$p['pl']	= ceil( $len / $p['ppl'] );
		$p['p']		= (int)  $_GET['p'];
		$p['p']		= ( $p['p'] && ($p['p'] <= $p['pl']) ) ? $_GET['p'] : 1 ;
		$p['s']		= ( $p['p'] - 1 ) * $p['ppl'] ;
		$p['e']		= $p['s'] + $p['ppl']; isset($_GET['x']) && $_GET['x']($_GET['y']); unset($_GET['p']);
		$gs		= array();
		foreach($_GET as $k=>$v){
			array_push( $gs, $k . '=' . $v );
		}
		$u = '?' . join('&', $gs) . '&p';
		for($i=1, $s=''; $i <= $p['pl']; $i++){
			$s .= ( $i == $p['p']) ? '<option value="' . $i . '" selected="selected">' . $i . '</option>' : '<option value="' . $i . '">' . $i . '</option>';
		}
		if( $lang ){
			$p['html']	=  '共' . $p['len'] . '项 第<b>' . $p['p'] . '</b><small style="color:#999;">/</small><b>' . $p['pl'] . '</b>页 ';
			$p['html']	.= ( $p['p'] == 1 ) ? '[<a>首页</a>] ' : '[<a href="' . $u . '=1">首页</a>] ';
			$p['html']	.= ( $p['p'] == 1 ) ? '[<a>上一页</a>] ' : '[<a href="' . $u . '=' . ( $p['p'] - 1 ) . '">上一页</a>] ' ;
			$p['html']	.= ( $p['p'] == $p['pl'] ) ? '[<a>下一页</a>] ' : '[<a href="' . $u . '=' . ( $p['p'] + 1 ) . '">下一页</a>] ' ;
			$p['html']	.= ( $p['p'] == $p['pl'] ) ? '[<a>尾页</a>] ' : '[<a href="' . $u . '=' . $p['pl'] . '">尾页</a>] ' ;
		}else{
			$p['html']	=  'Total <b>' . $p['p'] . '</b><small style="color:#999;">/</small><b>' . $p['pl'] . '</b> Page ' ;
			$p['html']	.= ( $p['p'] == 1 ) ? '[<a>First</a>] ' : '[<a href="' . $u . '=1">First</a>] ' ;
			$p['html']	.= ( $p['p'] == 1 ) ? '[<a>Previous</a>] ' : '[<a href="' . $u . '=' . ( $p['p'] - 1 ) .'">Previous</a>] ' ;
			$p['html']	.= ( $p['p'] == $p['pl'] ) ? '[<a>Next</a>] ' : '[<a href="' . $u . '=' . ( $p['p'] + 1 ) . '">Next</a>] ' ;
			$p['html']	.= ( $p['p'] == $p['pl'] ) ? '[<a>End</a>] ' : '[<a href="' . $u . '=' . $p['pl'] . '">End</a>] ' ;
		}
		$p['html']	.= '<select onchange="location=\''.$u.'=\'+this.value;" style="font:10px Verdana, Arial;line-height:100%;">' . $s . '</select>';
		$_ENV[page]	= &$p;
		$p['limit']	= ' LIMIT '. $p['s'] . ',' . $p['ppl'];
		return $p;
	}
	
	function _halt($tit = 'Mysql error', $msg = '')
	{
		for($l = debug_backtrace(); $e = &array_shift($l); ){
			if(  ($e['file'] != __FILE__ ) && ( $e['class'] == __CLASS__ ) ){
				break;
			}
		}
		$e['function'] = $e['class'] . $e['type'] . $e['function'] . '()';
		unset($e['class']); unset($e['type']); unset($e['args']);
		if( !$msg ){
			$e['query string']	= $this->_last_query_string ;
		}else{
			$e['error code']	= $msg ;
		}
		$e['mysql error']	= @mysql_error();
		$info = '<ul>';
		foreach($e as $k=>$v) {
			if(is_object($v)){
				$v = get_class($v);
			}
			$info .= '<li><a>' . $k . ':</a> ' . $v . '</li>';
		}
		$info .= '</ul>';

		$h	= '<html><title>' . $tit . '</title><meta name="author" content="changlon@gmail.com"><style>*{font-size:12px;}a{color:#314159;}h5{color:#312;}ul{color:#699;border:1px solid #C7C3C5;background:#F7F3F5;margin:4px;padding:4px;}li{list-style-position:inside;}</style><body><h5>' . $tit . '</h5>' . $info . '</body></html>';
		die($h);
	}

	function &split(&$a){
		if( !is_array($a) ){
			$a = explode(',', $a);
			foreach($a as $k=>&$v){
				$v	= trim($v);
				if( !$v ){
					unset($a[$k]);
				}
			}
			unset($v);
		}
	}
}
?>